<script setup lang="ts">
import { computed, ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { cloneDeep } from '@vben/utils';

import { useVbenForm } from '#/adapter/form';
import {
  digitalEmployeeAdd,
  digitalEmployeeInfo,
  digitalEmployeeUpdate,
} from '#/api/aisys/digitalEmployee';

import { modalSchema } from './data';
import type {DigitalEmployeeForm} from "#/api/aisys/digitalEmployee/model";
import type {RuleObject} from "ant-design-vue/es/form";
import {Form, FormItem} from "ant-design-vue";
import {Tinymce} from "#/components/tinymce";


const emit = defineEmits<{ reload: [] }>();

const isUpdate = ref(false);
const title = computed(() => {
  return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
});
const defaultValues: Partial<DigitalEmployeeForm> = {
  id: undefined,
  name: undefined,
  cue: undefined,
  aiModel: undefined,
  role: undefined,
  platform: undefined,
  sort: undefined,
  remark: undefined,
};

const formData = ref(defaultValues);

type AntdFormRules<T> = Partial<Record<keyof T, RuleObject[]>> & {
  [key: string]: RuleObject[];
};

const formRules = ref<AntdFormRules<DigitalEmployeeForm>>({
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  cue: [{ required: true, message: '请输入员工职责', trigger: 'blur' }],
  aiModel: [{ required: true, message: '请选择AI模型', trigger: 'change' }],
  role: [{ required: true, message: '请选择角色', trigger: 'change' }],
  platform: [{ required: true, message: '请选择平台标识', trigger: 'change' }],
  sort: [{ required: true, message: '请输入显示顺序', trigger: 'blur' }],
  remark: [{ required: true, message: '请输入备注', trigger: 'blur' }],
});

const { validate, validateInfos, resetFields } = Form.useForm(
  formData,
  formRules,
);

const [BasicForm, formApi] = useVbenForm({
  commonConfig: {
    // 默认占满两列
    formItemClass: 'col-span-2',
    // 默认label宽度 px
    labelWidth: 80,
    // 通用配置项 会影响到所有表单项
    componentProps: {
      class: 'w-full',
    },
  },
  schema: modalSchema(),
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
});

const [BasicModal, modalApi] = useVbenModal({
  // 在这里更改宽度
  class: 'w-[850px]',
  fullscreenButton: false,
  // 点击遮罩是否关闭
  closeOnClickModal: false,
  onCancel: handleCancel,
  onConfirm: handleConfirm,
  onOpenChange: async (isOpen) => {
    if (!isOpen) {
      return null;
    }
    modalApi.modalLoading(true);

    const { id } = modalApi.getData() as { id?: number | string };
    isUpdate.value = !!id;

    if (isUpdate.value && id) {
      const record = await digitalEmployeeInfo(id);
      await formApi.setValues(record);
    }

    modalApi.modalLoading(false);
  },
});

async function handleConfirm() {
  try {
    modalApi.modalLoading(true);
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    // getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
    const data = cloneDeep(await formApi.getValues());
    await (isUpdate.value
      ? digitalEmployeeUpdate(data)
      : digitalEmployeeAdd(data));
    emit('reload');
    await handleCancel();
  } catch (error) {
    console.error(error);
  } finally {
    modalApi.modalLoading(false);
  }
}

async function handleCancel() {
  modalApi.close();
  await formApi.resetForm();
}
</script>

<template>
  <BasicModal :title="title">

    <BasicForm />
  </BasicModal>
</template>
